package com.pet.server.ext.action.square;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.PetAdoptItem;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Adoptitem extends ActionBase{

	public Integer adopt_id;
	public Adoptitem(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		adopt_id=NumberUtil.getInteger(params.getFieldValue("adopt_id"));
		if(StringUtil.isEmpty(adopt_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		JSONObject data = new JSONObject();
		
		JSONArray array = new JSONArray();
		List<PetAdoptItem> list = userService.queryAdoptItem(adopt_id);
		for(PetAdoptItem pet:list){
			JSONObject petJson = new JSONObject();
			petJson.accumulate("nick_name",pet.nick_name);
			petJson.accumulate("user_id",pet.user_id);
			petJson.accumulate("ico_path",pet.ico_path);
			petJson.accumulate("sex",pet.sex);
			petJson.accumulate("phone",pet.phone);
			petJson.accumulate("created_time",JsonUtil.getTime(pet.created_time));
			petJson.accumulate("description", pet.description);
			array.add(petJson);
		}
		
		data.accumulate("list", array);
		Result result = new Result();
		result.put("data", data);
	    return result;
	}


}
